package com.ftwj.demo.modular.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.modular.entity.DeptBean;
import com.ftwj.demo.modular.service.DeptService;
import com.ftwj.demo.global.shiro.ShiroUtil;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;
import com.ftwj.demo.utils.validate.ValidateGroupKey;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* <p>Title: DeptController</p>  
* <p>Description: 部门管理前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author HeYang
* @date 2020年4月10日 下午4:39:10
* @version V1.0
 */
@RestController 
@RequestMapping("/dept")
@Api(tags="部门管理接口", value="部门管理")
@Validated
public class DeptController {
	
	@Autowired
	private DeptService service;

	/**
	 * <p>Title: tree</p>  
	 * <p>Description: 获取部门树</p>  
	 * @param pid
	 * @param size
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@GetMapping("tree")
	@ApiOperation(value="部门树数据", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> tree(@RequestParam(required = true) Integer pid, @RequestParam(required = true) Integer size) {
		List<Map<String, Object>> list = service.findlistMap(pid, size);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 部门分页查询</p>  
	 * @param pid
	 * @param current
	 * @param size
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="部门分页查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> page(@RequestParam(required = true) Integer pid,@RequestParam(required = true) Integer current,@RequestParam(required = true) Integer size) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,pid);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 部门列表查询</p>  
	 * @param pid
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("list/{pid}")
	@ApiOperation(value="部门列表查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.PATH, defaultValue="0")
	})
	public ResultBean<?> list(@PathVariable@Positive Integer pid) {
		List<DeptBean> list = service.list(pid);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 新增</p>  
	 * @param dict
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="部门新增", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated DeptBean dept) {
		dept.setCreateUser(ShiroUtil.getUserId()).setCreateTime(new Date());
		if(service.add(dept)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: update</p>  
	 * <p>Description: 部门修改</p>  
	 * @param dept
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PutMapping("/update")
	@ApiOperation(value="部门修改", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> update(@RequestBody@Validated(ValidateGroupKey.class) DeptBean dept) {
		dept.setCreateUser(ShiroUtil.getUserId()).setCreateTime(new Date());
		if(service.save(dept)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: delete</p>  
	 * <p>Description: 删除</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value="部门删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="部门id", dataType=DataType.INTEGER, paramType=ParamType.PATH)
	})
	public ResultBean<?> delete(@PathVariable@Positive Integer id) {
		if(service.remo(id)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
}
